/*
 * @Description: 
 * @Author: lgt
 * @Date: 2021-06-27 14:15:22
 * @LastEditTime: 2021-06-27 18:24:31
 * @LastEditors: xxx
 */
 const bcrypt = require('bcryptjs'); //密码加密包
const Router = require('koa-router');
const {success} = require("../../lib/helper")
const router = new Router(
    {
        prefix:'/v1/user'
    }
);

//导入校验器
const {RegisterValidator} = require("../../validators/validator")
//导入 model
const { User } = require("../../models/user")

router.post('/register',async(ctx,next)=>{
    //思维方式  接收参数、参数校验
    const v =await new RegisterValidator().validate(ctx); // 校验中有异步操作
    console.log("v",v)
    // 密码加密
    // const salt = bcrypt.genSaltSync(10);
    // const psw = bcrypt.hashSync(v.get('body.password1'),salt);
    //取出校验器中的数据
    const user = {
        email:v.get('body.email'),
        password:v.get('body.password1'),
        nickname:v.get('body.nickname')
    }

    await User.create(user)

    success();
    // ctx.body={
    //     msg:'新增成功'
    // }
})

module.exports = router;